package edu.zut.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class CalculateServlet extends HttpServlet {

    //doPost：自动映射post请求
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String num1 = request.getParameter("num_1");
        String num2 = request.getParameter("num_2");
        System.out.println(num1 + "==========" + num2);
        //计算二者之和
        int sum = Integer.parseInt(num1) + Integer.parseInt(num2);
        PrintWriter writer = response.getWriter();
        writer.write(num1 + "+" + num2 + "=" + sum);
        writer.flush();
        writer.close();
    }

    /**
     * doGet:自动映射get请求
     * url属于请求对象中的内容，url中的参数也属于request对象
     * @param request Http请求
     * @param response Http响应
     * @throws ServletException
     * @throws IOException
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //从request对象中获取请求参数
        String num1 = request.getParameter("num_1");
        String num2 = request.getParameter("num_2");
        System.out.println(num1 + "==========" + num2);
        //计算二者之和
        int sum = Integer.parseInt(num1) + Integer.parseInt(num2);
        PrintWriter writer = response.getWriter();
        writer.write(num1 + "+" + num2 + "=" + sum);
        writer.flush();
        writer.close();
    }
}
